package cn.supermaster.management.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Cookie工具类
 */
public class CookieUtils {

    private final static Logger logger = LoggerFactory.getLogger(CookieUtils.class);

    /**
     * 设置 Cookie（生成时间为1天）
     *
     * @param name  名称
     * @param value 值
     */
    public static void setCookie(HttpServletResponse response, String name, String value) {
        setCookie(response, name, value, 60 * 60 * 24);
    }

    /**
     * @Description: [设置 Cookie]
     * <p></P>
     * @Title: setCookie
     * @Author: Lance Ting
     * @Date: 2021-01-6
     * @Param: response
     * @Param: name 名称
     * @Param: value  值
     * @Param: path  生存时间（单位秒）
     * @Return: void
     */
    public static void setCookie(HttpServletResponse response, String name, String value, String path) {
        setCookie(response, name, value, path, 60 * 60 * 24);
    }

    /**
     * @Description: [设置 Cookie]
     * <p></P>
     * @Title: setCookie
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: response
     * @Param: name 名称
     * @Param: value 值
     * @Param: maxAge 生存时间（单位秒）
     * @Return: void
     */
    public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
        setCookie(response, name, value, "/", maxAge);
    }

    /**
     * 设置 Cookie
     *
     * @param name   名称
     * @param value  值
     * @param maxAge 生存时间（单位秒）
     */
    public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
        Cookie cookie = new Cookie(name, null);
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        try {
            cookie.setValue(URLEncoder.encode(value, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
        response.addCookie(cookie);
    }

    /**
     * @Description: [获得指定Cookie的值]
     * <p></P>
     * @Title: getCookie
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: request
     * @Param: name  名称
     * @Return: java.lang.String 值
     */
    public static String getCookie(HttpServletRequest request, String name) {
        return getCookie(request, null, name, false);
    }

    /**
     * @Description: [获得指定Cookie的值，并删除。]
     * <p></P>
     * @Title: getCookie
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: request
     * @Param: response
     * @Param: name  名称
     * @Return: java.lang.String 值
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        return getCookie(request, response, name, true);
    }

    /**
     * @Description: [获得指定Cookie的值]
     * <p></P>
     * @Title: getCookie
     * @Author: Lance Ting
     * @Date: 2021-01-06
     * @Param: request 请求对象
     * @Param: response 响应对象
     * @Param: name 名字
     * @Param: isRemove 是否移除
     * @Return: java.lang.String 值
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
                                   boolean isRemove) {
        String value = null;
        try {
            Cookie[] cookies = request.getCookies();
            if (null == cookies) return null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    value = URLDecoder.decode(cookie.getValue(), "utf-8");
                    if (isRemove) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return value;
    }
}